<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\HasDatabaseNotifications;
use App\Events\SendNotice;
use Carbon\Carbon;


class User extends Model
{
    const STATUS_ABLED = 1; //正常
    const STATUS_DISABLED = 2; //用户被冻结，禁止登陆

    const DE_AVATAR = 'images/common/201907/20/2_1563608116_mnbtRcPp4S.png';

    protected $guarded = [];

    protected $hidden = [
        'password', 'security', 'accessToken',
    ];


    public function notify($instance) {
        
        $this->increment('notification_count');            
        
        // event(new SendNotice($instance,2));
    }
    
    public function markAsRead() {
        $this->notification_count = 0;
        $this->save();
        // $this->notifications->update(['read_at'=>Carbon::now()]); 
    }
    
    public function notifications()
    {
        return $this->hasMany(Notification::class);
    }

    public function getAvatarAttribute()
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($this->attributes['avatar'], ['http://', 'https://'])) {
            return $this->attributes['avatar'];
        }
        return config('app.img_url').$this->attributes['avatar'];
    }

    public function wallet()
    {
        return $this->hasOne(Wallet::class);
    }

    public function rentcloth()
    {
        return $this->hasOne(RentCloth::class);
    }

    public function member()
    {
        return $this->hasOne(Member::class, 'id', 'level');
    }

    public function powerLog()
    {
        return $this->hasOne(PowerLog::class);
    }

    public function Recharge()
    {
        return $this->hasMany(Recharge::class);
    }

    public function Withdraw()
    {
        return $this->hasMany(Withdraw::class);
    }

    public function dealLog()
    {
        return $this->hasOne(DealLog::class, 'user_id', 'id');
    }

    public function shop()
    {
        return $this->hasMany(Shop::class);
    }

    public function shopRole()
    {
        return $this->hasMany(Shop_role::class);
    }

    public function usercard()
    {
        return $this->hasOne(UserCard::class, 'used_by', 'id');
    }
    public function useraddr()
    {
        return $this->hasMany(UserAddr::class);
    }

}
